package elitech.vietnam.myfashion.dialog;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import org.xmlpull.v1.XmlPullParserException;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import elitech.vietnam.myfashion.MainActivity;
import elitech.vietnam.myfashion.R;
import elitech.vietnam.myfashion.doinbackground.CallServiceTask;
import elitech.vietnam.myfashion.widgets.ScaleImageView;
import elitech.vietnam.myfashion.wsclient.ImageUploader;
import elitech.vietnam.myfashion.wsclient.Service;

public class NewPostDialog extends Dialog implements android.view.View.OnClickListener {
	
	private static final String POST_DIR = "images/post/";
	
	private MainActivity mContext;
	
	private Button mBtnCreate, mBtnCancel;
	private ScaleImageView mImage;
	private EditText mTextBox;
	private String imagePath;
	
	boolean scaled;
	
	public NewPostDialog(MainActivity context, String image, boolean scaled) {
		super(context, R.style.Dialog);
		this.mContext = context;
		this.imagePath = image;
		this.scaled = scaled;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newpost);
		mImage = (ScaleImageView) findViewById(R.id.newpost_image);
		mTextBox = (EditText) findViewById(R.id.newpost_textbox);
		mBtnCreate = (Button) findViewById(R.id.newpost_create);
		mBtnCancel = (Button) findViewById(R.id.newpost_cancel);
		mBtnCancel.setOnClickListener(this);
		mBtnCreate.setOnClickListener(this);
		
		Bitmap bmp;
		Options option = new Options();
		option.inSampleSize = 2;
		
		bmp = BitmapFactory.decodeFile(imagePath, option);
		mImage.setImageBitmap(bmp);
		
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
	}

	public void onUploadDone() {};
	
	@Override
	public void onClick(View v) {
		mBtnCreate.setEnabled(false);
		InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(mTextBox.getWindowToken(), 0);
		
		switch (v.getId()) {
		case R.id.newpost_create:
			CreatePost();
			break;

		case R.id.newpost_cancel:
			dismiss();
			break;
			
		default:
			break;
		}
		
	}
	
	@Override
	public void dismiss() {
		File file = new File(MainActivity.IMAGE_TEMP);
		if (file.exists())
			file.delete();
		super.dismiss();
	}
	
	private void CreatePost() {
		new CallServiceTask() {
			int result = -1;
			public void work() {
				String name = mContext.mUser.Id + "_" + System.currentTimeMillis() + ".jpg";
				String image = POST_DIR + name;
				
				ImageUploader uploader = new ImageUploader();
				
				boolean uploadOk = false;
				uploadOk = uploader.upload(imagePath, name, null);
				
				if (uploadOk) {
					String title = mContext.mUser.Id + "_" + mContext.mUser.Name + " Style " + new Date().toString();
					Service service = new Service();
					try {
						result = service.newStyle(mContext.mUser.Id, title, image, mTextBox.getText().toString());
						Log.w("Shopping", "New Style: Id: "+mContext.mUser.Id+" Title: "+title+" Image: "+image+" Contend: "+ mTextBox.getText().toString());
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			};
			protected void onPostExecute(Void result) {
				if (this.result<0){
					Toast.makeText(mContext, mContext.getString(R.string.connecterror), Toast.LENGTH_SHORT).show();
					mBtnCreate.setEnabled(true);
				}
				else{
					final int rs = this.result;
					Toast.makeText(mContext, mContext.getString(R.string.postcreated), Toast.LENGTH_SHORT).show();
					dismiss();
					new AsyncTask<Integer, Integer, Boolean>() {
						@Override
						protected Boolean doInBackground(Integer... params) {
							try {
								new Service().notifyFriendPost(rs);
							} catch (IOException | XmlPullParserException e) {
								e.printStackTrace();
							}
							return null;
						}
					};
					onUploadDone();
				}
			}
		}.execute();
	}
}
